/**
 * =================================================
 *
 * @copyright 杭州龙骞科技有限公司 2012-1014
 * =================================================
 */
package com.boilerCloud.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置文件读写工具类
 * 
 * @author qfq
 * @date 2012-10-16 15:44:34
 * @version 1.0
 */
public class Configuration
{
    
    private static final Logger log = LoggerFactory.getLogger(Configuration.class);
    
    private static Properties properties = null;
    static
    {
        log.info("配置文件初始化开始");
        FileInputStream fileis;
        try
        {
            Map<String, String> m = System.getenv();
            String path = m.get("TOMCAT_HOME") + "/conf/preferentialLifeManager.properties";
            path = path.replaceAll("//", File.separator);
            log.info("配置文件初始化路径:{}", path);
            
            fileis = new FileInputStream(path);
            
            properties = new Properties();
            properties.load(fileis);
            
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            log.error("配置文件初始化异常:{}", e);
        }
    }
    
    /**
     * 读取配置文件信息
     * 
     * @param name 读取节点名
     * @param fileName 文件名
     * @return 读取的节点值
     */
    public static String readConfigString(String name, String fileName)
    {
        String result = "";
        try
        {
            ResourceBundle rb = ResourceBundle.getBundle(fileName);
            result = rb.getString(name);
        }
        catch (Exception e)
        {
            log.error("从" + fileName + "读取" + name + "出错:" + e.getMessage());
            return properties.getProperty(name);
        }
        return result;
    }
    
    /**
     * 读取配置文件信息
     * @param key  读取节点名
     * @return 读取的节点值
     */
    public static String readConfigString(String key)
    {
        String result = "";
        if (properties != null)
        {
            return properties.getProperty(key);
        }
        return result;
    }
    
}
